import { parse } from 'querystring';
/* eslint no-useless-escape:0 import/prefer-default-export:0 */

const reg = /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/;
export const isUrl = path => reg.test(path);
export const isAntDesignPro = () => {
  if (ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION === 'site') {
    return true;
  }

  return window.location.hostname === 'preview.pro.ant.design';
}; // 给官方演示站点用，用于关闭真实开发环境不需要使用的特性

export const isAntDesignProOrDev = () => {
  const { NODE_ENV } = process.env;

  if (NODE_ENV === 'development') {
    return true;
  }

  return isAntDesignPro();
};
export const getPageQuery = () => parse(window.location.href.split('?')[1]);

export const getToken = () => {
  const authorization = localStorage.getItem('authorization');
  try {
    if (authorization) {
      return JSON.parse(authorization);
    }
  } catch (e) {
    console.error(e);
  }
  return false;
};

export const setToken = authorization => {
  localStorage.setItem('authorization', JSON.stringify(authorization));
};

export const flushToken = () => {
  localStorage.removeItem('authorization');
};

export const getAuthorization = () => {
  const token = getToken();
  return token ? `${token.token_type} ${token.access_token}` : '';
};

// 文件上传联合表单 getValueFromEvent 使用
export const normFile = (e, done, error, uploading) => {
  if (typeof error === 'function' && e.file.status === 'error') error(e);

  if (typeof uploading === 'function' && e.file.status === 'uploading') uploading(e);

  if (typeof done === 'function' && e.file.status === 'done') done(e);

  return e && e.fileList.filter(file => file.status === 'done').map(file => file.uid);
};
